﻿namespace Stickam_Client
{
    [StandardModule]
    private __gc sealed class Globals
    {
        // Methods
        public: static String __gc* AddSplitters(String __gc* Data, Int32 __gc* UnknownValue)
        {
            String __gc* str2 = S"";
            if (Data->Length > 0x80)
            {
                Char __gc* ch = Strings::Chr((0xc0 + UnknownValue));
                Int32 __gc* length = Data->Length;
                for (Int32 __gc* i = 1; (i <= length); i += 0x80)
                {
                    String __gc* str3 = Strings::Mid(Data, i, 0x80);
                    if (str3->Length == 0x80)
                    {
                        str3 = String::Concat(str3, Conversions::ToString(ch));
                    }
                    str2 = String::Concat(str2, str3);
                }
                return str2;
            }
            return Data;
        }

        public: static Int32 __gc* BytesToInt(String __gc* ByteStr)
        {
            Int32 __gc* num2;
            Byte __gc* bytes __gc [] = Globals::Encoder->GetBytes(Strings::StrReverse(ByteStr));
            GCHandle __gc* handle = GCHandle::Alloc(num2, GCHandleType::Pinned);
            IntPtr __gc* destination = handle->AddrOfPinnedObject();
            Marshal::Copy(bytes, 0, destination, bytes->Length);
            num2 = Marshal::ReadInt32(destination);
            handle->Free();
            return num2;
        }

        public: static String __gc* CleanSplitters(String __gc* Data, Int32 __gc* Stepper)
        {
            if (Stepper == 0x85)
            {
                Data = Strings::Mid(Data, 5);
            }
            String __gc* str2 = S"";
            Int32 __gc* num3 = Stepper;
            Int32 __gc* length = Data->Length;
            for (Int32 __gc* i = 1; (((num3 >> 0x1f) ^ i) <= ((num3 >> 0x1f) ^ length)); i += num3)
            {
                str2 = String::Concat(str2, Strings::Mid(Data, i, 0x80));
            }
            return str2;
        }

        public: static Color __gc* ColorFromString(String __gc* ColorStr)
        {
            if (ColorStr == S"Default")
            {
                return Globals::DefaultColor;
            }
            if (Globals::IsHex(ColorStr) & (ColorStr->Length == 6))
            {
                return Color::FromArgb(Conversions::ToInteger(String::Concat(Globals::H, Strings::Mid(ColorStr, 1, 2))), Conversions::ToInteger(String::Concat(Globals::H, Strings::Mid(ColorStr, 3, 2))), Conversions::ToInteger(String::Concat(Globals::H, Strings::Mid(ColorStr, 5, 2))));
            }
            return Color::FromName(ColorStr);
        }

        public: static String __gc* ColorToHex(Color __gc* ColorObj)
        {
            return String::Concat(Globals::FixedHex(ColorObj->R), Globals::FixedHex(ColorObj->G), Globals::FixedHex(ColorObj->B));
        }

        private: static String __gc* FixedHex(Int32 __gc* IntVal)
        {
            String __gc* str2 = Conversion::Hex(IntVal);
            return String::Concat(Strings::Mid(Globals::B, 1, (2 - str2->Length)), str2);
        }

        public: static String __gc* GetParam(String __gc* ParamName, String __gc* Data, String __gc* Ender)
        {
            ParamName = String::Concat(ParamName, S"=");
            String __gc* str = S"";
            Int32 __gc* num = Strings::InStr(Data, ParamName, CompareMethod::Binary);
            if (num > 0)
            {
                str = Strings::Mid(Data, (num + ParamName->Length));
                str = Strings::Mid(str, 1, (Strings::InStr(str, Ender, CompareMethod::Binary) - 1));
            }
            return str;
        }

        public: static String __gc* HttpGet(String __gc* HostName, String __gc* FilePath, String __gc* Cookie)
        {
            String __gc* str = S"";
            if (Cookie->Length > 0)
            {
                str = String::Concat(S"Cookie: ", Cookie, S"\r\n");
            }
            return String::Concat(__gc new String __gc*[7] {
                S"GET ", FilePath, S" HTTP/1.1\r\n", str, S"Host: ", HostName, S"\r\nConnection: Close\r\n\r\n"});
        }

        public: static String __gc* IntToBytes(Int32 __gc* IntValue, Int32 __gc* BytesTotal)
        {
            Byte __gc* destination __gc [] = __gc new Byte __gc*[((BytesTotal - 1) + 1)];
            GCHandle __gc* handle = GCHandle::Alloc(IntValue, GCHandleType::Pinned);
            Marshal::Copy(handle->AddrOfPinnedObject(), destination, 0, BytesTotal);
            handle->Free();
            return Strings::StrReverse(Globals::Encoder->GetString(destination));
        }

        public: static Boolean __gc* IsHex(String __gc* HexStr)
        {
            HexStr = HexStr->ToUpper();
            Int32 __gc* length = HexStr->Length;
            for (Int32 __gc* i = 1; (i <= length); i++)
            {
                if (Strings::InStr(Globals::HexChars, Strings::Mid(HexStr, i, 1), CompareMethod::Binary) == 0)
                {
                    return false;
                }
            }
            return true;
        }

        public: static Boolean __gc* IsValidIP(String __gc* IP)
        {
            Boolean __gc* flag2;
            Int32 __gc* num5;
            try
            {
                Int32 __gc* num6;
            Label_0000:
                ProjectData::ClearProjectError();
                Int32 __gc* num4 = -2;
            Label_0009:
                num6 = 2;
                Boolean __gc* flag = true;
            Label_000E:
                num6 = 3;
                String __gc* strArray __gc [] = Strings::Split(IP, S":", -1, CompareMethod::Binary);
            Label_001F:
                num6 = 4;
                if (strArray->Length != 2)
                {
                    goto Label_00CF;
                }
            Label_002B:
                num6 = 5;
                Int32 __gc* num = 0;
            Label_0030:
                num6 = 6;
                num = Conversions::ToInteger(strArray[1]);
            Label_003C:
                num6 = 7;
                if (!((num > 0) & (num < 0x10000)))
                {
                    goto Label_00C3;
                }
            Label_004E:
                num6 = 8;
                String __gc* strArray2 __gc [] = Strings::Split(strArray[0], S".", -1, CompareMethod::Binary);
            Label_0062:
                num6 = 9;
                if (strArray2->Length != 4)
                {
                    goto Label_00B7;
                }
            Label_006D:
                num6 = 10;
                Int32 __gc* index = 0;
            Label_0074:
                num6 = 11;
                Int32 __gc* num2 = -1;
            Label_007B:
                num6 = 12;
                num2 = Conversions::ToInteger(strArray2[index]);
            Label_008B:
                num6 = 13;
                if (!((num2 < 0) | (num2 > 0xff)))
                {
                    goto Label_00A6;
                }
            Label_00A0:
                num6 = 14;
                flag = false;
            Label_00A6:
                num6 = 0x10;
                index++;
                if (index <= 3)
                {
                    goto Label_0074;
                }
                goto Label_00D9;
            Label_00B7:
                num6 = 0x12;
            Label_00BB:
                num6 = 0x13;
                flag = false;
                goto Label_00D9;
            Label_00C3:
                num6 = 0x16;
            Label_00C7:
                num6 = 0x17;
                flag = false;
                goto Label_00D9;
            Label_00CF:
                num6 = 0x1a;
            Label_00D3:
                num6 = 0x1b;
                flag = false;
            Label_00D9:
                num6 = 0x1d;
                flag2 = flag;
                goto Label_01BE;
            Label_00E9:
                num5 = 0;
                switch ((num5 + 1))
                {
                    case 1:
                    {
                        goto Label_0000;
                    }
                    case 2:
                    {
                        goto Label_0009;
                    }
                    case 3:
                    {
                        goto Label_000E;
                    }
                    case 4:
                    {
                        goto Label_001F;
                    }
                    case 5:
                    {
                        goto Label_002B;
                    }
                    case 6:
                    {
                        goto Label_0030;
                    }
                    case 7:
                    {
                        goto Label_003C;
                    }
                    case 8:
                    {
                        goto Label_004E;
                    }
                    case 9:
                    {
                        goto Label_0062;
                    }
                    case 10:
                    {
                        goto Label_006D;
                    }
                    case 11:
                    {
                        goto Label_0074;
                    }
                    case 12:
                    {
                        goto Label_007B;
                    }
                    case 13:
                    {
                        goto Label_008B;
                    }
                    case 14:
                    {
                        goto Label_00A0;
                    }
                    case 15:
                    case 0x10:
                    {
                        goto Label_00A6;
                    }
                    case 0x11:
                    case 20:
                    case 0x15:
                    case 0x18:
                    case 0x19:
                    case 0x1c:
                    case 0x1d:
                    {
                        goto Label_00D9;
                    }
                    case 0x12:
                    {
                        goto Label_00B7;
                    }
                    case 0x13:
                    {
                        goto Label_00BB;
                    }
                    case 0x16:
                    {
                        goto Label_00C3;
                    }
                    case 0x17:
                    {
                        goto Label_00C7;
                    }
                    case 0x1a:
                    {
                        goto Label_00CF;
                    }
                    case 0x1b:
                    {
                        goto Label_00D3;
                    }
                    case 30:
                    {
                        goto Label_01BE;
                    }
                    default:
                    {
                        goto Label_01B3;
                    }
                }
            Label_0173:
                num5 = num6;
                switch (((num4 > -2) ? num4 : 1))
                {
                    case 0:
                    {
                        goto Label_01B3;
                    }
                    case 1:
                    {
                        goto Label_00E9;
                    }
                }
            }
            catch (Object __gc* obj1) when (?)
            {
                ProjectData::SetProjectError(*static_cast<__box Exception*>(obj1));
                goto Label_0173;
            }
        Label_01B3:
            throw ProjectData::CreateProjectError(-2146828237);
        Label_01BE:
            if (num5 != 0)
            {
                ProjectData::ClearProjectError();
            }
            return flag2;
        }

        public: static String __gc* LoadFile(String __gc* FileName)
        {
            String __gc* str = S"";
            if (File::Exists(FileName))
            {
                FileStream __gc* stream = File::OpenRead(FileName);
                Byte __gc* array __gc [] = __gc new Byte __gc*[(*static_cast<__box Int32*>((stream->Length - 1)) + 1)];
                stream->Read(array, 0, *static_cast<__box Int32*>(stream->Length));
                stream->Close();
                str = Globals::Encoder->GetString(array);
            }
            return str;
        }

        public: static Object __gc* RandomNumber(Int32 __gc* Min, Int32 __gc* Max)
        {
            VBMath::Randomize();
            return Conversion::Int(*static_cast<__box Single*>(((((Max - Min) + 1) * VBMath::Rnd()) + Min)));
        }

        public: static String __gc* RndByte()
        {
            Globals::Randomizer = Conversions::ToInteger(Operators::AddObject(Globals::Randomizer, Globals::RandomNumber(1, 0xff)));
            Globals::Randomizer = (Globals::Randomizer % 0xff);
            if (Globals::Randomizer == 0)
            {
                Globals::Randomizer++;
            }
            return Conversions::ToString(Strings::Chr(Globals::Randomizer));
        }

        public: static String __gc* RtmpHandshake()
        {
            String __gc* str = String::Concat(__gc new String __gc*[5] {
                S"", Globals::RndByte(), Globals::RndByte(), Globals::RndByte(), S"\t\0|"});
            Int32 __gc* num = 1;
            do
            {
                str = String::Concat(str, Globals::RndByte());
                num++;
            }
            while ((num <= 0x5f8));
            return str;
        }

        public: static void __gc* SaveFile(String __gc* FileName, String __gc* Data)
        {
            FileStream __gc* stream = File::Open(FileName, FileMode::Create);
            stream->Write(Globals::Encoder->GetBytes(Data), 0, Data->Length);
            stream->Close();
        }


        // Fields
        private: static String __gc* B = S"00";
        public: static Color __gc* DefaultColor = Color::FromArgb(0xc0, 0xc0, 0xc0);
        public: static Encoding __gc* Encoder = Encoding::Default;
        public: static Encoding __gc* EncoderUTF8 = Encoding::UTF8;
        private: static String __gc* H = S"&H";
        private: static String __gc* HexChars = S"0123456789ABCDEF";
        private: static Int32 __gc* Randomizer = 0;
    };
}

